package com.gdepri.cabelwellserver.server;
import com.gdepri.cabelwellserver.entity.CableMessageSegment;
import com.gdepri.cabelwellserver.entity.CableMessageSegmentExample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

@Component
public class Server {
    private Logger logger = LoggerFactory.getLogger(Server.class);

    //@Autowired
    //CableMessageSegmentService service;
    {
        System.out.println("Server已经被注入");
    }

    // 初始化方法
    public void init() throws IOException {
        //TestMibatis();
        int port = 18889;
        //1.创建 ServerSocket的对象，同时为服务端创建端口
        ServerSocket serverSocket = new ServerSocket(port);
        logger.info("电缆井服务端v1启动成功,监听端口"+port);
        while(true){
            //2.使用ServerSocket的对象，调用accept方法，等待客户端的连接
            Socket socket = serverSocket.accept();

            logger.info("有设备上线了："+socket.getRemoteSocketAddress());
            //new ServerReaderThread(socket,service).start();
            new ServerReaderThread(socket).start();
        }
    }

//    public void TestMibatis(){
//        CableMessageSegmentExample ex = new CableMessageSegmentExample();
//        CableMessageSegmentExample.Criteria criteria = ex.createCriteria();
//        criteria.andIdLessThan(200);
//        List<CableMessageSegment> cableMessageSegments = service.selectByExample(ex);
//        System.out.println(cableMessageSegments);
//    }

}
